<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/userInfo' }">用户管理</el-breadcrumb-item>
        </el-breadcrumb>
        <br>
        <el-row>
          <el-col :span="6">
            <el-input
                    placeholder="请输入内容"
                    prefix-icon="el-icon-search"
                    size="mini"
                    v-model="search">
            </el-input>
          </el-col>

          <el-col :span="6">
             <el-button size="mini" @click="searchUserInfo" type="primary">搜索</el-button>
          </el-col>

          <el-col :span="12">
            <el-button @click="addUser" style="float: right; padding: 3px 0" type="text">+新增员工</el-button>
          </el-col>
        </el-row>



      </div>

      <div>
        <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                tooltip-effect="dark"
                @selection-change="handleSelectionChange"
                style="width: 100%">
          <el-table-column
                  type="selection"
                  width="55">
          </el-table-column>
          <!--<el-table-column
                  sortable
                  label="Id"
                  prop="id">
          </el-table-column>-->
          <el-table-column
                  label="姓名"
                  prop="name">
          </el-table-column>
          <el-table-column
                  label="账户名"
                  prop="account">
          </el-table-column>
          <el-table-column
                  label="性别"
                  prop="sex">
            <template slot-scope="scope">
              {{scope.row.sex==='1'?'男':'女'}}
            </template>
          </el-table-column>
          <el-table-column
                  label="部门"
                  prop="departmentId">
            <template slot-scope="scope">
              <div v-for="d in department">
                {{ String(d.departmentId)===String(scope.row.departmentId)?d.departmentName:''}}
              </div>
            </template>
          </el-table-column>
          <el-table-column
                  label="联系电话"
                  prop="mobile">
          </el-table-column>
          <el-table-column
                  label="邮箱"
                  prop="email">
          </el-table-column>
          <el-table-column
                  label="生日"
                  prop="birthday">
          </el-table-column>
          <el-table-column
                  label="创建时间"
                  prop="createTime">
          </el-table-column>
          <el-table-column
                  fixed="right"
                  align="right">
            <template slot="header" slot-scope="scope">
              <el-button
                      size="mini"
                      type="danger"
                      style="width: 100px"
                      @click="handleDeletes(scope.$index, scope.row)">DeleteSelect</el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                      size="mini"
                      type="success"
                      style="width: 80px"
                      @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
                <el-button
                      size="mini"
                      type="danger"
                      style="width: 80px"
                      @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage4"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page_total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
  import {getUser, delUser, findUserByName, getDepartmentId} from "../../network/UserInfo";

  export default {
    name: "UserInfo",
    data() {
      return {
        tableData: [],
        search: '',
        currentPage4: 1,
        page: {
          pageNo: 1,
          pageSize: 5
        },
        page_total: 0,
        account: '',
        loading: true,
        multipleSelection: [],
        department: []
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(val)
      },
      // 修改用户信息
      handleEdit(index, row) {
        console.log(row)
        this.$store.commit('setUserData', row)
        this.$router.push("/userEdit")
      },
      // 删除用户信息
      handleDelete(index, row) {
        // this.account = row.account
        delUser(row.id).then(res => {
              this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                if (row.totalUser===0){
                  getUser(this.page).then(res => {
                        this.loading = true
                        this.tableData = res.data.content
                        this.page_total = res.data.content.length;
                        this.page_total = res.data.totalElements
                        setTimeout(() => {
                          this.$message.success('删除成功')
                          this.loading = false
                        },500)
                      },
                      err => {
                        console.log(err);
                      })
                }else {
                  this.$message.success('部门中含有员工禁止删除')
                }
              }).catch(() => {
                setTimeout(() => {
                  console.log(typeof row.totalUser);
                  this.$message.success('取消删除')
                  this.loading = false
                },500)
              });
            },
        err => {
          console.log(err);
        })

      },
      // 批量删除用户信息
      handleDeletes() {
        this.$confirm('此操作将批量删除用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let x of this.multipleSelection) {
            delUser(x.account).then(res => {
              getUser(this.page).then(res => {
                    this.loading = true
                    this.tableData = res.data.content
                    this.page_total = res.data.content.length;
                    this.page_total = res.data.totalElements
                    setTimeout(() => {
                      this.loading = false
                    },500)
                  },
                  err => {
                    console.log(err);
                  })
            })
          }
          this.$message({
            type: 'success',
            message: '批量删除成功!'
          });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消批量删除'
          });
        });
      },
      // 修改分页的大小
      handleSizeChange(val) {
        this.page.pageSize = val
        getUser(this.page).then(res => {
              this.loading = true
              this.tableData = res.data.content
              this.page_total = res.data.content.length;
              this.page_total = res.data.totalElements
              setTimeout(() => {
                this.loading = false
              },500)
            },
            err => {
              console.log(err);
            })
      },
      // 新增用户
      addUser() {
        this.$router.push("/addUser")
      },
      // 修改当前按页数
      handleCurrentChange(val) {
        this.page.pageNo = val
        getUser(this.page).then(res => {
              this.loading = true
              this.tableData = res.data.content
              this.page_total = res.data.content.length;
              this.page_total = res.data.totalElements
              setTimeout(() => {
                this.loading = false
              },500)
            },
            err => {
              console.log(err);
            })
      },
      // 搜索用户
      searchUserInfo() {
        findUserByName(this.search).then(res => {
          console.log(res);
          this.tableData = res.data
        })
      },
      getDepartmentId() {
        getDepartmentId().then(res => {
          this.department = res.data
        })
      }
    },
    mounted() {
      this.getDepartmentId()
      getUser(this.page).then(res => {
            this.tableData = res.data.content;
            this.page_total = res.data.content.length;
            this.page_total = res.data.totalElements
        setTimeout(() => {
          this.loading = false
        },500)
          },
          err => {
            console.log(err);
          })
    }
  }
</script>

<style scoped>

</style>
